package com.itheima.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.HomeRecommendProduct;
import com.itheima.domain.HomeRecommendSubject;
import com.itheima.mapper.HomeRecommendSubjectMapper;
import com.itheima.service.IHomeRecommendSubjectService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 首页推荐专题表 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class HomeRecommendSubjectServiceImpl extends ServiceImpl<HomeRecommendSubjectMapper, HomeRecommendSubject> implements IHomeRecommendSubjectService {

    @Autowired
    HomeRecommendSubjectMapper homeRecommendSubjectMapper;

    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {
        LambdaQueryWrapper<HomeRecommendSubject> lqw=new LambdaQueryWrapper<>();
        Page<HomeRecommendSubject> page=new Page<>(pageNum,pageSize);

        lqw.like(subjectName!=null,HomeRecommendSubject::getSubjectName,subjectName);
        lqw.eq(recommendStatus!=null,HomeRecommendSubject::getRecommendStatus,recommendStatus);
        lqw.orderByDesc(HomeRecommendSubject::getSort);
        homeRecommendSubjectMapper.selectPage(page,lqw);
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }

    @Override
    public void addNew(HomeRecommendSubject homeRecommendSubject) {
        homeRecommendSubjectMapper.insert(homeRecommendSubject);

    }

    @Override
    public void delete(Integer[] ids) {
        if(ArrayUtil.isAllEmpty(ids))
        {
            for (Integer id : ids) {
                homeRecommendSubjectMapper.deleteById(id);

            }
        }
    }

    @Override
    public void update(Integer[] ids, Integer recommendStatus) {
        if(ArrayUtil.isNotEmpty(ids))
        {
            for (Integer id : ids) {
                HomeRecommendSubject homeRecommendSubject = homeRecommendSubjectMapper.selectById(id);
                homeRecommendSubject.setRecommendStatus(recommendStatus);
                homeRecommendSubjectMapper.updateById(homeRecommendSubject);

            }
        }
    }

    @Override
    public void sort(Integer id, Integer sort) {
        HomeRecommendSubject homeRecommendSubject = homeRecommendSubjectMapper.selectById(id);
        homeRecommendSubject.setSort(sort);
        homeRecommendSubjectMapper.updateById(homeRecommendSubject);
    }
}
